<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class ome_ctl_admin_accountperiod extends desktop_controller{
    
    var $workground = 'ome_ctl_admin_accountperiod';
    var $pagelimit = 10;
    var $accountperiod_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->accountperiod_model = $this->app->model('accountperiod');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
   function index(){
   	
        $custom_actions[] = array('label'=>app::get('ome')->_('初始化账期'),'href'=>'index.php?app=ome&ctl=admin_accountperiod&act=add_accountperiod','target'=>'dialog::{title:\''.app::get('初始化账期')->_('添加').'\',width:460,height:240}');
        $custom_actions[] = array('label'=>app::get('ome')->_('测试'),'submit'=>'index.php?app=ome&ctl=admin_accountperiod&act=test');


        $actions_base['title'] = app::get('ome')->_('快递费账期');
        $actions_base['actions'] = $custom_actions;
        $actions_base['allow_detail_popup'] = true;
        $actions_base['use_buildin_set_tag'] = false;
        $actions_base['use_buildin_export'] = false;
        $actions_base['use_buildin_filter'] = false;
        $actions_base['use_buildin_recycle'] = false;

        $actions_base['use_view_tab'] = false;

        $this->finder('ome_mdl_accountperiod',$actions_base);
    }
    function test()
    {
        $this->begin('index.php?app=ome&ctl=admin_accountperiod&act=index');
        $gift_info='tbgift' => array ( 'gift' => array ( 0 => array ( 'type' => 'unpaid', 'unpaidprice' => '', 'price' => NULL, ), 1 => array ( 'oid' => '267746883809070069', 'type' => 'category', 'cid' => '50018994', 'price' => 99.9, ), 2 => array ( 'oid' => '267746883810070069', 'type' => 'category', 'cid' => '50023728', 'price' => 99, );

        $orderObj = app::get('ome')->model('orders');
        $tbgiftOrderItemsObj = app::get('ome')->model('tbgift_order_items');
        $tmp_arr = $gift_info;
        foreach((array)$tmp_arr as $info){
            if($info['type'] == 'gift'){
                $data = array(
                    'order_id' => $orderid,
                    'outer_item_id' => $info['id'],
                    'name' => $info['name'],
                    'nums' => $info['num'],
                    'price' => $info['price'],                    
                );
                if($tbgiftOrderItemsObj->save($data)){
                    $is_update = true;
                }
            }
        }

        $this->end(true, app::get('base')->_('初始化成功'));
    }
    function test1()
    {

        $this->begin('index.php?app=ome&ctl=admin_accountperiod&act=index');

         //发货完成表
        $deliverycomp_model = app::get('wms')->model('deliverycomp');
        $deliverycomp_delis_model = app::get('wms')->model('deliverycomp_line');
        $deliveryMdl = app::get('wms')->model('delivery');
        $deliveryItemsMdl = app::get('wms')->model('delivery_items');
        $shop_obj = app::get('ome')->model('shop');
        $sales_material = app::get('material')->model('basic_material');
        $sales_material_ext = app::get('material')->model('basic_material_ext');
        $orderItemsMdl = app::get('ome')->model('order_items');
        $orderMdl = app::get('ome')->model('orders');
        $comp_data = $deliverycomp_model->getDeliveryInfo('4447');
        var_dump($comp_data);
        exit;
        if ($comp_data) 
        {
            foreach ($comp_data as $cv) 
            {
                $deliveryItemsData = $deliveryItemsMdl->getList('*', array('delivery_id' => $cv['delivery_id']));
                // $orderIdArr = $deliveryMdl->getOrderIdByDeliveryId($cv['delivery_id']);
                if (!empty($deliveryItemsData)) {
                    foreach ($deliveryItemsData as $v) {
                        $salesMData = $sales_material->dump(array('material_bn' => $v['bn']), 'bm_id');
                        $unit = $sales_material_ext->dump(array('bm_id' => $salesMData['bm_id']), 'unit');

                      

                        $divide_order_fee=$this->getdividefeenew($cv['order_id'],$v['bn']);
                        if($divide_order_fee[0]['divide_order_fee']==0)
                        {
                            print($cv['delivery_bn'].'发货单价格存在异常！');
                            return false;
                        }
                        $SalesPrice = round(($divide_order_fee[0]['divide_order_fee']/$divide_order_fee[0]['nums']),4);
                    
                        $tmp = $shop_bn .'-'.$shopData['shop_bn'].'-' . $v['bn'] . '-' . $SalesPrice . '-' . $unit['unit'] . '-' . $cv['axshipno'] . '-' . $cv['axorderno'] . '-' . $cv['carno'];
                        $salesPlatfrom = '其他';
                        if ($shopData['shop_type'] != '') {
                            $salesPlatfrom = ome_shop_type::shop_name($shopData['shop_type']);
                        }
                        if (isset($number_tmp[$tmp])) {
                            $number_tmp[$tmp] += $v['number'];
                        } else {
                            $number_tmp[$tmp] = $v['number'];
                        }
                        if($cv['axorderno']=='ESG318111115' && $v['bn']=='MR3100U0504'){
                            $xml[$tmp] = '<Cust>'
                                . '<CustAccount>' . $shop_bn . '</CustAccount>'
                                . '<ItemId>' . $v['bn'] . '</ItemId>'
                                . '<SalesPrice>' . $SalesPrice . '</SalesPrice>'
                                . '<SalesQty>' . $number_tmp[$tmp]. '</SalesQty>'
                                . '<SalesUnit>' . $unit['unit'] . '</SalesUnit>'
                                . '<PackingSlipId>' . $cv['axshipno'] . '</PackingSlipId>'
                                . '<SalesId>' . $cv['axorderno'] . '</SalesId>'
                                . '<LicencePlateNo>' . $cv['carno'] . '</LicencePlateNo>'
                                . '<shopName>' . $shopData['name'] . '</shopName>'
                                . '<salesPlatfrom>' . $salesPlatfrom . '</salesPlatfrom>'
                                . '<shop_bn>' . $shopData['shop_bn'] . '</shop_bn>'
                                . '</Cust>';
                        }
                    }
                }
            }
        }
        foreach ($xml as $key => $value) {
            $rel_xml .= $value;
        }
        var_dump(json_encode($rel_xml));
        $this->end(true, app::get('base')->_('初始化成功'));

        //     $bldata = array(
        //     'op_id' => 16777210, 
        //     'op_name' => 'system',
        //     'createtime' => time(),
        //     'batch_number' => 1,
        //     'log_type' => 'syncoms',
        //     'log_text'=>serialize(array(
        //     'dlycompid' => 'testdlycomp',
        //     )),
        // );
        // $blObj = app::get('wms')->model('batch_log');
        // $result = $blObj->save($bldata);
        // //同步发货完成任务加队列
        // $push_params = array(
        //     'data' => array(
        //         'log_text' => $bldata['log_text'],
        //         'log_id' => $bldata['log_id'],
        //         'task_type' => 'autodlycomp'
        //     ),
        //     'url' => kernel::openapi_url('openapi.autotask', 'service')
        // );
        // $res=kernel::single('taskmgr_interface_connecter')->push($push_params);
        // var_dump($res);
    }
    function arrayToXml($data)
    {
        if(!is_array($data) || count($data) <= 0){
            return false;
        }
        $xml = "<xml>";
        foreach ($data as $key=>$val){
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml; 
    }
    //保存应收快递费
    function add()
    {
        $this->begin('index.php?app=ome&ctl=admin_accountperiod&act=index');

        if(empty($_POST['year']))
        {
            $this->end(false, app::get('base')->_('无参数，请确认！'));
        }

        $accountperiod_data=$this->accountperiod_model->getList('*', array( 'year'=> $_POST['year']));
        if(!empty($accountperiod_data))
        {
            $this->end(false, app::get('base')->_($_POST['year'].'已启用过，请确认！'));   
        }

        for ($i=1; $i < 13; $i++) 
        { 
            $arr_order = array();        
            $arr_order['year'] = $_POST['year'];
            $arr_order['month'] = $i;
            $this->accountperiod_model->save($arr_order);
        }

        $this->end(true, app::get('base')->_('初始化成功'));
    }
    /**
     * 设置默认仓库
     * 
     * @param Integer $bid 仓库ID
     * @return void 
     */
    function setApprove($id,$type) 
    {
        $oOperation_log = app::get('ome')->model('operation_log');
        if($type==0)
        {
            $msg='启用成功！！！';
            $sql="update sdb_ome_accountperiod set status='1',op_id='".kernel::single ( 'desktop_user' )->get_id ()."',start_time=".time()."  where id=".$id;
            
        }else{

            $msg='禁用成功！！！';
            $sql="update sdb_ome_accountperiod set status='0',op_id=null,start_time=null  where id=".$id;
        }
        $oOperation_log->write_log('accountperiod@ome',$id,'快递费账期操作：'.$msg);
        kernel::database()->exec($sql);
        echo "<script>alert('$msg');top.finderGroup['{$_REQUEST[finder_id]}'].refresh();</script>";
    } 

    /**
     * 设置默认仓库
     * 
     * @param Integer $bid 仓库ID
     * @return void 
     */
    function setclose($id,$type) 
    {
        $oOperation_log = app::get('ome')->model('operation_log');
        if($type==0)
        {
            $msg='关账成功！！！';
            $sql="update sdb_ome_accountperiod set status='2',close_id='".kernel::single ( 'desktop_user' )->get_id ()."',close_time=".time()."  where id=".$id;
        }else{
            $msg='取消关账成功！！！';
            $sql="update sdb_ome_accountperiod set status='1',close_id=null,close_time=null  where id=".$id;
        }
        $oOperation_log->write_log('accountperiod@ome',$id,'快递费账期操作：'.$msg);
        kernel::database()->exec($sql);
        
        echo "<script>alert('$msg');top.finderGroup['{$_REQUEST[finder_id]}'].refresh();</script>";
    } 
    function add_accountperiod()
    {

        $this->pagedata['title'] = '初始化账期';
        $this->page('admin/accountperiod/new.html');
    }


}
